<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CourseOrder;
use App\Admin\RowActions\CourseRefund;
use App\Constants\CourseOrderStatusConstant;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CourseOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CourseOrder(), function (Grid $grid) {
            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('mini_type', array_keys($map))->orderByDesc('id');

            $grid->column('id');
            $grid->column('member_id');
            $grid->column('mini_type')->display(function ($miniType) use ($map) {
                return $miniType ? $map[$miniType] : '';
            });
            $grid->column('order_sn');
            $grid->column('pay_sn');
            $grid->column('pay_amount', '支付金额')->display(function ($v) {
                return fen_to_yuan($v);
            });
            $grid->column('pay_time')->display(function ($v) {
                return $v > 0 ? date('Y-m-d H:i:s', $v) : '';
            });
            $grid->column('status', '支付状态')->using(CourseOrderStatusConstant::MAP)->dot(['danger', 'success', 'warning', 'info']);
            $grid->column('course_id');
            $grid->column('course_name');
            $grid->column('course_cover')->image(' ', 50);
            $grid->column('course_price', '课程金额')->display(function ($v) {
                return fen_to_yuan($v);
            });
            $grid->column('course_vip_price', '课程vip金额')->display(function ($v) {
                return fen_to_yuan($v);
            });
            $grid->column('created_at');

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if ($actions->row->status == CourseOrderStatusConstant::IS_PAY) {
                    $actions->append(new CourseRefund(\App\Models\CourseOrder::class));
                }
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(2);
                $filter->equal('member_id')->width(2);
                $filter->like('order_sn')->width(2);
                $filter->like('pay_sn')->width(2);
                $filter->equal('course_id')->width(2);
                $filter->like('course_name')->width(2);
                $filter->equal('status', '状态')->select(CourseOrderStatusConstant::MAP)->width(2);
                $filter->between('created_at')->datetime()->width(4);
            });

            $grid->disableCreateButton();
            $grid->disableViewButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CourseOrder(), function (Show $show) {
            $show->field('id');
            $show->field('member_id');
            $show->field('mini_type');
            $show->field('order_sn');
            $show->field('pay_sn');
            $show->field('pay_amount');
            $show->field('pay_time');
            $show->field('status');
            $show->field('course_id');
            $show->field('course_name');
            $show->field('course_cover');
            $show->field('course_price');
            $show->field('course_vip_price');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CourseOrder(), function (Form $form) {
            $form->display('id');
            $form->text('member_id');
            $form->text('mini_type');
            $form->text('order_sn');
            $form->text('pay_sn');
            $form->text('pay_amount');
            $form->text('pay_time');
            $form->text('status');
            $form->text('course_id');
            $form->text('course_name');
            $form->text('course_cover');
            $form->text('course_price');
            $form->text('course_vip_price');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
